<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$group=pdo_getall('jy_dzs_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$groupid=intval($_RQ['groupid']);
	if($groupid){
		$params['groupid']=$groupid;
		$return['params']['groupid']=$groupid;
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_dzs_list',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);			
			$item['url']=custom_url('jy_dzs/detail//id='.$item['id'],true);
		}
	}
	$return['list']=$list?$list:array();
	$return['group']=$group?$group:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'电子书列表'),
			array('op'=>'edit','title'=>'添加电子书'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：电子书，由图片翻页；如果大PDF生成电子书报错：联系管理员安装ghostscript扩展','list'=>array(
			array('name'=>'title','title'=>'电子书标题','type'=>'string'),
			array('type'=>'select','name'=>'groupid','options'=>'allgroups','title'=>'绑定分类'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types'),
			array('name'=>'readnum','title'=>'阅卷量','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'电子书标题','type'=>'string'),
			array('type'=>'select','width'=>10,'name'=>'groupid','options'=>'allgroups','title'=>'绑定分类'),
		),
	);
	$basic=$this->plugin_setting('basic');
	if($basic['ispi']){
		$htmls['tabs'][]=array('op'=>'plan','title'=>'批量生成');
	}
	$return['types']=$this->types;
	$return['allgroups']=pdo_getall('jy_dzs_group',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('jy_dzs_list',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_dzs_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_dzs_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_dzs_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('status'=>1,'type'=>0);
	}
	$item['images']=json_decode($item['images'],true);
	$item['oldpdf']=$item['pdf'];
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回电子书列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'title','title'=>'名称','type'=>'string','must'=>true),
					array('type'=>'select','name'=>'groupid','options'=>'allgroups','title'=>'绑定分类'),
					array('name'=>'image','title'=>'logo','type'=>'image'),
					array('name'=>'desc','title'=>'描述','type'=>'text'),
					array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types'),
					array('name'=>'images','title'=>'轮播图','type'=>'images','show'=>array('type'=>0)),
					array('name'=>'pdf','title'=>'PDF文档','type'=>'upfile','show'=>array('type'=>1)),
					array('name'=>'bgmp3','title'=>'背景音乐','type'=>'audio'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
		)
	);
	$return['types']=$this->types;
	$return['duicuo']=$this->duicuo;
	$return['allgroups']=pdo_getall('jy_dzs_group',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$fields=array('title','image','images','desc','groupid','bgmp3','type','pdf','status');
	$data=array_fields($fields,$postdata);
	if(empty($data['title'])){
		exi('请填写电子书标题！','error');
	}
	if(empty($data['images']) && !$data['type']){
		exi('请上传图片！！','error');
	}
	if(empty($data['pdf']) && $data['type']){
		exi('请上传PDF！！','error');
	}
	$basic=$this->plugin_setting('basic');
	if($data['type'] && $postdata['oldpdf']!=$data['pdf'] && !$basic['ispi']){
		$imgs=$postdata['images'];
		$data['images']=$this->model('model')->pdf2png($data['pdf'],'',$imgs);
	}
	$data['images']=ijson_encode($data['images']);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_dzs_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('编辑电子书成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_dzs_list',$data);
		$id=pdo_insertid();
		exi('添加电子书成功！','','list');
	}
}
if($_RQ['op']=='plan'){
	$return=array();
	$return['item']=array();
	$htmls=array(
		'type'=>'edit',
		'post'=>'postplan',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回电子书列表'),
		),
		'edit'=>array(
			array('title'=>'[温馨提示]：TXT,PDF文档不会自动生成电子书;需要这里点击批量生成才行！','type'=>'tip'),
			array('name'=>'type','title'=>'批量生成操作','type'=>'radio','options'=>'types'),
		)
	);
	$return['types']=array('只生成未生成的文档','只生成昨天以前的所有电子书');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='postplan'){
	$postdata=get_postdata();
	if($_RQ['update']){
		$postdata['type']=$_RQ['type'];
	}
	$params=array('type'=>1,'uniacid'=>$_SESSION['uniacid']);
	if(!$postdata['type']){
		$params['isup']=0;
	}else{
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$params['uptime <']=$daytime;
	}
	$item=pdo_get('jy_dzs_list',$params);
	if($item['id']){
		exi("准备生成【{$item['title']}】，请勿关闭页面！",'','//update/id='.$item['id'].'&type='.$postdata['type']);
	}else{
		exi('已批量生成完成！','','list');
	}
}
if($_RQ['op']=='update'){
	$item=pdo_get('jy_dzs_list',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	$imgs=json_decode($item['images'],true);
	$item['images']=$this->model('model')->pdf2png($item['pdf'],'',$imgs);
	$update=array(
		'images'=>ijson_encode($item['images']),
		'isup'=>1,
		'uptime'=>time(),
	);
	pdo_update('jy_dzs_list',$update,array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
	exi('','','//postplan/update=1&type='.$_RQ['type']);
}